package cn.wolfcode.web.controller;

import cn.wolfcode.ann.RequiredPermission;
import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private  IDepartmentService departmentService;
    @Autowired
    private IEmployeeService employeeService;
    public DepartmentController(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }
    @RequiredPermission(name="部门页面",expression = "department:list")
    @RequestMapping("/list")
    //Model 用来封装数据
    public String list(Model model,  QueryObject qo) {
        //改成PageInfo封装
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";
    }

@RequiredPermission(name="新增/编辑",expression = "department:saveOrUpdate")
@RequestMapping("/input")
    public String input(Long id, Model model){
        if (id!=null){
            Department department=departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }
    @RequiredPermission(name="新增/编辑",expression = "department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
       if (department.getId()==null){
           departmentService.save(department);
       }else {
           departmentService.update(department);
       }
        return "redirect:/department/list";
    }
    @RequiredPermission(name="部门删除",expression = "department:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            departmentService.delete(id);
        }
        return "redirect:/department/list";
    }
}
